package com.foreverwin.jzymes.common.service.impl;

import com.foreverwin.jzymes.common.mapper.RouterStepAdviceMapper;
import com.foreverwin.jzymes.common.service.IRouterStepAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class RouterStepAdviceServiceImpl implements IRouterStepAdviceService {

    @Autowired
    RouterStepAdviceMapper routerStepAdviceMapper;

    /**
     * 查询工艺路线工序前面工序列表
     *
     * @param site
     * @param routerBo
     * @param operation
     * @return
     */
    public List<String> findPrevOperationBoList( String site, String routerBo, String operation ){

        String operationHandle = findOperationHandle( site, operation );
        List<String> prevOperationBoList = new ArrayList<String>();
        boolean exist = false;
        List<Map<String,Object>> routerStepList = findRouterStepList( routerBo );
        for( Map<String,Object> routerStep : routerStepList ){
            /**跳过完成工序*/
            if( ((String)routerStep.get( "ROUTER_COMP_GBO" )).startsWith( "RouterDoneStepBO" ) ){
                continue;
            }
            Map<String,Object> routerOperationMap = findRouterOperationMap((String) routerStep.get( "HANDLE" ));
            if( operationHandle.equals( routerOperationMap.get( "OPERATION_BO" ) ) ){
                exist = true;
                break;
            }
            if( !exist ){
                prevOperationBoList.add((String) routerOperationMap.get( "OPERATION_BO" ));
            }
        }
        /**检查工序在工艺路线是否存在*/
        Assert.isTrue( exist, I18nUtil.getI18nText( "common.operationNotInRouter", new String[]{ operation, routerBo } ) );
        return prevOperationBoList;
    }

    /**
     * 查询工艺路线工序前面工序列表
     *
     * @param site
     * @param routerBo
     * @param operation
     * @return
     */
    public List<String> findPrevOperationList( String site, String routerBo, String operation ){

        String operationHandle = findOperationHandle( site, operation );
        List<String> prevOperationList = new ArrayList<String>();
        boolean exist = false;
        List<Map<String,Object>> routerStepList = findRouterStepList( routerBo );
        for( Map<String,Object> routerStep : routerStepList ){
            /**跳过完成工序*/
            if( ((String)routerStep.get( "ROUTER_COMP_GBO" )).startsWith( "RouterDoneStepBO" ) ){
                continue;
            }
            Map<String,Object> routerOperationMap = findRouterOperationMap((String) routerStep.get( "HANDLE" ));
            if( operationHandle.equals( routerOperationMap.get( "OPERATION_BO" ) ) ){
                exist = true;
                break;
            }
            if( !exist ){
                prevOperationList.add( BOHandleUtil.splitOperationHandle( (String) routerOperationMap.get( "OPERATION_BO" ) )[1] );
            }
        }
        /**检查工序在工艺路线是否存在*/
        Assert.isTrue( exist, I18nUtil.getI18nText( "common.operationNotInRouter", new String[]{ operation, routerBo } ) );
        return prevOperationList;
    }

    public String findOperationHandle( String site, String operation ){
        /**检查站点*/
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        /**检查工序*/
        Assert.hasText( operation, I18nUtil.getI18nText( "common.operationNotBlank" ) );

        String operationHandle = BOHandleUtil.buildOperationHandleCurrRev( site, operation );
        return operationHandle;
    }

    /**
     * 查询工艺路线工序后面工序列表
     *
     * @param site
     * @param routerBo
     * @param operation
     * @return
     */
    public List<String> findNextOperationBoList( String site, String routerBo, String operation ){
        String operationHandle = findOperationHandle( site, operation );
        List<String> nextOperationBoList = new ArrayList<String>();
        boolean exist = false;
        List<Map<String,Object>> routerStepList = findRouterStepList( routerBo );
        for( Map<String,Object> routerStep : routerStepList ){
            /**跳过完成工序*/
            if( ((String)routerStep.get( "ROUTER_COMP_GBO" )).startsWith( "RouterDoneStepBO" ) ){
                continue;
            }
            Map<String,Object> routerOperationMap = findRouterOperationMap((String) routerStep.get( "HANDLE" ));
            if( operationHandle.equals( routerOperationMap.get( "OPERATION_BO" ) ) ){
                exist = true;
                continue;
            }
            if( exist ){
                nextOperationBoList.add((String) routerOperationMap.get( "OPERATION_BO" ));
            }
        }
        /**检查工序在工艺路线是否存在*/
        Assert.isTrue( exist, I18nUtil.getI18nText( "common.operationNotInRouter", new String[]{ operation, routerBo } ) );
        return nextOperationBoList;
    }

    /**
     * 查询工艺路线工序后面工序列表
     *
     * @param site
     * @param routerBo
     * @param operation
     * @return
     */
    public List<String> findNextOperationList( String site, String routerBo, String operation ){
        String operationHandle = findOperationHandle( site, operation );
        List<String> nextOperationList = new ArrayList<String>();
        boolean exist = false;
        List<Map<String,Object>> routerStepList = findRouterStepList( routerBo );
        for( Map<String,Object> routerStep : routerStepList ){
            /**跳过完成工序*/
            if( ((String)routerStep.get( "ROUTER_COMP_GBO" )).startsWith( "RouterDoneStepBO" ) ){
                continue;
            }
            Map<String,Object> routerOperationMap = findRouterOperationMap((String) routerStep.get( "HANDLE" ));
            if( operationHandle.equals( routerOperationMap.get( "OPERATION_BO" ) ) ){
                exist = true;
                continue;
            }
            if( exist ){
                nextOperationList.add( BOHandleUtil.splitOperationHandle( (String) routerOperationMap.get( "OPERATION_BO" ) )[1] );
            }
        }
        /**检查工序在工艺路线是否存在*/
        Assert.isTrue( exist, I18nUtil.getI18nText( "common.operationNotInRouter", new String[]{ operation, routerBo } ) );
        return nextOperationList;
    }

    /**
     * 查询工艺路线步骤列表
     *
     * @param routerBo
     * @return
     */
    public List<Map<String,Object>> findRouterStepList( String routerBo ){
        /**检查工艺路线Handle*/
        Assert.hasText( routerBo, I18nUtil.getI18nText( "common.routerBoNotBlank" ));
        List<Map<String,Object>> routerStepList = routerStepAdviceMapper.selectRouterStepList( routerBo );
        Assert.isTrue( routerStepList != null && routerStepList.size() > 0, I18nUtil.getI18nText("common.routerStepNotExist", new String[]{ routerBo } ) );
        return routerStepList;
    }

    /**
     * 查询工艺路线步骤对应工序
     *
     * @param routerStepBo
     * @return
     */
    public Map<String,Object> findRouterOperationMap( String routerStepBo ){
        /**检查工艺步骤handle*/
        Assert.hasText( routerStepBo, I18nUtil.getI18nText( "common.routerStepBoNotBlank" ) );
        Map<String,Object> routerOperationMap = routerStepAdviceMapper.selectRouterOperationMap( routerStepBo );
        Assert.notNull( routerOperationMap, I18nUtil.getI18nText( "common.routerOperationNotNull", new String[]{ routerStepBo } ) );
        return routerOperationMap;
    }

    /**
     * 查询工艺路线步骤前一步骤
     *
     * @param routerStepBo
     * @return
     */
    public Map<String,Object> findPrevStepMap( String routerStepBo ){
        /**检查工艺步骤handle*/
        Assert.hasText( routerStepBo, I18nUtil.getI18nText( "common.routerStepBoNotBlank" ) );
        Map<String,Object> routerStepMap = routerStepAdviceMapper.selectPrevStepMap( routerStepBo );
        return routerStepMap;
    }

    /**
     * 查询工艺路线后一步骤
     *
     * @param routerStepBo
     * @return
     */
    public Map<String,Object> findNextStepMap( String routerStepBo ){
        /**检查工艺步骤handle*/
        Assert.hasText( routerStepBo, I18nUtil.getI18nText( "common.routerStepBoNotBlank" ) );
        Map<String,Object> routerStepMap = routerStepAdviceMapper.selectNextStepMap( routerStepBo );
        return routerStepMap;
    }
}
